package com.yystar.support.util;

import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import com.yystar.support.base.service.SpringApplicationContext;
import com.yystar.support.base.util.AppUtility;
import com.yystar.support.clock.service.ClockService;
import com.yystar.user.operator.domain.OperatorPolicy;

@Component
public class WAppUtility {
	public static final String LONG_DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	private static NumberFormat decimal0Formatter = WTextUtility.getNumberFormatter(0, false);
	private static NumberFormat decimal2Formatter = WTextUtility.getNumberFormatter(2, false);
	private static NumberFormat decimal3Formatter = WTextUtility.getNumberFormatter(3, false);
	private static NumberFormat onlieReportFormatter = WTextUtility.getNumberFormatter(0, true);
	private static NumberFormat percentFormat = NumberFormat.getPercentInstance();
	public static String APP_NAME = "SB2(BP)";

	private static ThreadPoolExecutor threadPool = null;

	static {
		threadPool = new ThreadPoolExecutor(2, 4, 200,

		TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2),

		new ThreadPoolExecutor.DiscardOldestPolicy());
	}

	public static String name() {
		return "SB2(BP)";
	}

	public static String version() {
		return "2.3.0.0070";
	}

	public static String currentDatabaseDateTime() {
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(((ClockService) SpringApplicationContext.getBean("clockService")).getCurrentDatabaseDateTime());
	}

	public static String currentDatabaseDate() {
		return new SimpleDateFormat("yyyy-MM-dd").format(((ClockService) SpringApplicationContext.getBean("clockService")).getCurrentDatabaseDateTime());
	}

	public static String currentTime() {
		return new SimpleDateFormat("HH:mm:ss").format(((ClockService) SpringApplicationContext.getBean("clockService")).getCurrentDatabaseDateTime());
	}

	public static boolean compareDate(Date date) {
		Date currentDate = ((ClockService) SpringApplicationContext.getBean("clockService")).getCurrentDatabaseDateTime();
		if (currentDate.after(date))
			return true;

		return false;
	}

	public static double random() {
		return Math.random();
	}

	public static int getCollectionSize(Collection<?> collection) {
		if (collection == null)
			return 0;
		return collection.size();
	}

	public static int getMapSize(Map<?, ?> map) {
		if (map == null)
			return 0;
		return map.size();
	}

	public static boolean isAhMarket(String marketTemplateCode) {
		if (StringUtils.isEmpty(marketTemplateCode))
			return false;

		return StringUtils.equals(marketTemplateCode, "AH") || StringUtils.equals(marketTemplateCode, "RAH");
	}

	public static boolean isOuMarket(String marketTemplateCode) {
		if (StringUtils.isEmpty(marketTemplateCode))
			return false;

		return StringUtils.equals(marketTemplateCode, "OU") || StringUtils.equals(marketTemplateCode, "ROU");
	}

	public static String showMarketPrice(Boolean spreadMarket, Double price) {
		if (spreadMarket) {
			return decimal3Formatter.format(price);
		}
		return decimal2Formatter.format(price);
	}

	public static String show2Decimal(double number) {
		return decimal2Formatter.format(number);
	}

	public static String show0Decimal(Double decimal) {
		return decimal0Formatter.format(decimal);
	}

	public static String showStake(Double stake) {
		return decimal2Formatter.format(stake);
	}

	public static String showOnlineReport(Double stake) {
		String numberString = "";
		if (stake > 0)
			numberString = "<span class=\"positiveNumberColor\">" + onlieReportFormatter.format(stake) + "</span>";
		else if (stake < 0)
			numberString = "<span class=\"negetiveNumberColor\">" + onlieReportFormatter.format(stake) + "</span>";
		else
			numberString = onlieReportFormatter.format(stake);
		return numberString;
	}

	public static String percentFormat(Double number) {
		return percentFormat.format(number);
	}

	public static double min2(double d1, double d2) {
		double result = 0d;

		if (d1 < d2) {
			result = d1;
		} else {
			result = d2;
		}

		return result;
	}

	public static double max2(double d1, double d2) {
		double result = 0d;

		if (d1 > d2) {
			result = d1;
		} else {
			result = d2;
		}

		return result;
	}

	public static double min3(double d1, double d2, double d3) {
		double result = 0d;

		if (d1 < d2) {
			if (d1 < d3) {
				result = d1;
			} else {
				result = d3;
			}
		} else {
			if (d2 < d3) {
				result = d2;
			} else {
				result = d3;
			}
		}

		return result;
	}

	/**
	 * Collection<E> E must String
	 * 
	 * @param collection
	 * @param o
	 * @return
	 */
	public static Boolean contains(Collection<?> collection, Object o) {
		if (collection == null || o == null)
			return false;
		return collection.contains(o.toString());
	}

	public static SimpleDateFormat getLongDateTimeFormat() {
		return new SimpleDateFormat(WAppUtility.LONG_DATE_TIME_FORMAT);
	}

	public static String generateToken(String opLoginCode, java.util.Date date, String clientIpAddr) {
		String token = "";
		try {
			token = WTextUtility.digestMD5(opLoginCode + APP_NAME + getLongDateTimeFormat().format(date) + clientIpAddr);
		} catch (Exception e) {
			AppUtility.recordException(e);
		}
		return token;
	}


	public static boolean canAccess(String operatorPolicy) {
		return OperatorPolicy.canAccess(OperatorPolicy.valueOf(operatorPolicy));
	}

	public static boolean isNumeric(String str) {
		Pattern pattern = Pattern.compile("[0-9]*");
		return pattern.matcher(str).matches();
	}

}
